package com.ourteam.product.model.dao.impl;

import com.ourteam.product.model.dao.IProductModelServiceParameterTemplateDAO;
import com.ourteam.product.model.dao.ProductModelServiceParameterTemplate;
import com.ourteam.product.model.dao.ProductModelServiceParameterTemplateDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 IRKT 2017
 * Auto Generate Date : 2017-11-17 11:01:19
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductModelServiceParameterTemplateDAOJdbcImpl
    extends AbstractSpringDAO
    implements IProductModelServiceParameterTemplateDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ServiceTemplateItemId, ParameterType, DataTypeId, DataCheck,
            IsArray, ParameterNameTemplate, SortIndex, Status, Remarks,
            DataTypeIdExpression, DataType
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ServiceTemplateItemId,
            _DataObjectProperty.ParameterType, _DataObjectProperty.DataTypeId,
            _DataObjectProperty.DataCheck, _DataObjectProperty.IsArray,
            _DataObjectProperty.ParameterNameTemplate,
            _DataObjectProperty.SortIndex, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks,
            _DataObjectProperty.DataTypeIdExpression,
            _DataObjectProperty.DataType
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductModelServiceParameterTemplate, ProductModelServiceParameterTemplateDAOQueryBean> daoHelper;

    public AbstractProductModelServiceParameterTemplateDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductModelServiceParameterTemplate, ProductModelServiceParameterTemplateDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.model.dao.IProductModelObjectDAO.EntityName)
                                .append(" RELATED_DATA_TYPE ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.model.dao.IProductModelObjectDAO.Id,
                    com.ourteam.product.model.dao.IProductModelObjectDAO.EntityName +
                    ".", "RELATED_DATA_TYPE."));
            relatedJoinSQLBuffer.append(" = ").append(DataTypeId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductModelServiceParameterTemplate
         * @throws Exception
         */
    public boolean checkIsUnique(
        ProductModelServiceParameterTemplate aProductModelServiceParameterTemplate)
        throws Exception {
        if (aProductModelServiceParameterTemplate == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductModelServiceParameterTemplate.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductModelServiceParameterTemplate
     * @throws Exception
     */
    public void insert(
        ProductModelServiceParameterTemplate aProductModelServiceParameterTemplate)
        throws Exception {
        super.doInsert(aProductModelServiceParameterTemplate);
    }

    /**
         * Batch Insert Datas
         * @param aProductModelServiceParameterTemplate
         * @throws Exception
         */
    public void batchInsert(
        ProductModelServiceParameterTemplate[] aProductModelServiceParameterTemplates)
        throws Exception {
        super.doBatchInsert(aProductModelServiceParameterTemplates);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(
        ProductModelServiceParameterTemplateDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(
        ProductModelServiceParameterTemplate aProductModelServiceParameterTemplate)
        throws Exception {
        delete(aProductModelServiceParameterTemplate.getId());
    }

    /**
     * Update Data Selective
     * @param aProductModelServiceParameterTemplate
     * @throws Exception
     */
    public void update(
        ProductModelServiceParameterTemplate aProductModelServiceParameterTemplate)
        throws Exception {
        if (aProductModelServiceParameterTemplate.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductModelServiceParameterTemplate);

        ProductModelServiceParameterTemplate tempObj = findById(aProductModelServiceParameterTemplate.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductModelServiceParameterTemplate,
                tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductModelServiceParameterTemplates[]
     * @throws Exception
     */
    public void batchUpdate(
        ProductModelServiceParameterTemplate[] aProductModelServiceParameterTemplates)
        throws Exception {
        super.doBatchUpdate(aProductModelServiceParameterTemplates);
    }

    /**
         * Update Data By Condictions
         * @param aProductModelServiceParameterTemplate
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(
        ProductModelServiceParameterTemplate aProductModelServiceParameterTemplate,
        ProductModelServiceParameterTemplateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aProductModelServiceParameterTemplate, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(
        ProductModelServiceParameterTemplateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(
        ProductModelServiceParameterTemplate aProductModelServiceParameterTemplate)
        throws Exception {
        if (aProductModelServiceParameterTemplate.getParameterType() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.parameterType value is not allow NULL");
        }

        if (aProductModelServiceParameterTemplate.getDataCheck() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.dataCheck value is not allow NULL");
        }

        if (aProductModelServiceParameterTemplate.getIsArray() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.isArray value is not allow NULL");
        }

        if (aProductModelServiceParameterTemplate.getParameterNameTemplate() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.parameterNameTemplate value is not allow NULL");
        }

        if (aProductModelServiceParameterTemplate.getStatus() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.status value is not allow NULL");
        }

        if (aProductModelServiceParameterTemplate.getRemarks() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.remarks value is not allow NULL");
        }

        if (aProductModelServiceParameterTemplate.getDataTypeIdExpression() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.dataTypeIdExpression value is not allow NULL");
        }

        if (aProductModelServiceParameterTemplate.getDataType() == null) {
            throw new Exception(
                "The ProductModelServiceParameterTemplate.dataType value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductModelServiceParameterTemplate findById(final long id)
        throws Exception {
        ProductModelServiceParameterTemplateDAOQueryBean queryBean = new ProductModelServiceParameterTemplateDAOQueryBean();

        ProductModelServiceParameterTemplateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductModelServiceParameterTemplate[] ProductModelServiceParameterTemplates =
            query(queryBean);

        if (ProductModelServiceParameterTemplates.length == 0) {
            return null;
        } else {
            return ProductModelServiceParameterTemplates[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductModelServiceParameterTemplate[]
         * @throws Exception
         */
    public ProductModelServiceParameterTemplate[] query(
        ProductModelServiceParameterTemplateDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductModelServiceParameterTemplateRowMapper());

        return (ProductModelServiceParameterTemplate[]) result.toArray(new ProductModelServiceParameterTemplate[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductModelServiceParameterTemplateDAOQueryBean queryBean)
        throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(
        ProductModelServiceParameterTemplateDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(
        ProductModelServiceParameterTemplate[] aProductModelServiceParameterTemplates,
        ProductModelServiceParameterTemplateDAOQueryBean queryBean)
        throws Exception {
        daoHelper.synchronize(aProductModelServiceParameterTemplates, queryBean);
    }

    public void synchronize(
        ProductModelServiceParameterTemplate[] aProductModelServiceParameterTemplates,
        ProductModelServiceParameterTemplateDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductModelServiceParameterTemplate> callBack)
        throws Exception {
        daoHelper.synchronize(aProductModelServiceParameterTemplates,
            queryBean, callBack);
    }

    public static class JdbcProductModelServiceParameterTemplateRowMapper
        implements RowMapper<ProductModelServiceParameterTemplate> {
        public ProductModelServiceParameterTemplate mapRow(
            ResultSet resultSet, int arg1) throws SQLException {
            ProductModelServiceParameterTemplate object = new ProductModelServiceParameterTemplate();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object serviceTemplateItemId = null;

                serviceTemplateItemId = resultSet.getObject(columnIndex++);

                object.setServiceTemplateItemIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (serviceTemplateItemId != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ServiceTemplateItemId,
                        serviceTemplateItemId);
                }

                Object parameterType = null;

                parameterType = resultSet.getObject(columnIndex++);

                object.setParameterTypeNull(resultSet.getBoolean(columnIndex++));

                if (parameterType != null) {
                    setBeanValue(object, _DataObjectProperty.ParameterType,
                        parameterType);
                }

                Object dataTypeId = null;

                dataTypeId = resultSet.getObject(columnIndex++);

                object.setDataTypeIdNull(resultSet.getBoolean(columnIndex++));

                if (dataTypeId != null) {
                    setBeanValue(object, _DataObjectProperty.DataTypeId,
                        dataTypeId);
                }

                Object dataCheck = null;

                dataCheck = resultSet.getObject(columnIndex++);

                object.setDataCheckNull(resultSet.getBoolean(columnIndex++));

                if (dataCheck != null) {
                    setBeanValue(object, _DataObjectProperty.DataCheck,
                        dataCheck);
                }

                Object isArray = null;

                isArray = resultSet.getObject(columnIndex++);

                object.setIsArrayNull(resultSet.getBoolean(columnIndex++));

                if (isArray != null) {
                    setBeanValue(object, _DataObjectProperty.IsArray, isArray);
                }

                Object parameterNameTemplate = null;

                parameterNameTemplate = resultSet.getObject(columnIndex++);

                object.setParameterNameTemplateNull(resultSet.getBoolean(
                        columnIndex++));

                if (parameterNameTemplate != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ParameterNameTemplate,
                        parameterNameTemplate);
                }

                Object sortIndex = null;

                sortIndex = resultSet.getObject(columnIndex++);

                object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

                if (sortIndex != null) {
                    setBeanValue(object, _DataObjectProperty.SortIndex,
                        sortIndex);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object dataTypeIdExpression = null;

                dataTypeIdExpression = resultSet.getObject(columnIndex++);

                object.setDataTypeIdExpressionNull(resultSet.getBoolean(
                        columnIndex++));

                if (dataTypeIdExpression != null) {
                    setBeanValue(object,
                        _DataObjectProperty.DataTypeIdExpression,
                        dataTypeIdExpression);
                }

                Object dataType = null;

                dataType = resultSet.getObject(columnIndex++);

                object.setDataTypeNull(resultSet.getBoolean(columnIndex++));

                if (dataType != null) {
                    setBeanValue(object, _DataObjectProperty.DataType, dataType);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
